import React from 'react'
import { Button, Modal, message } from 'antd'
import ModalFunc from '../../../components/modal/modal'
import { useDispatch } from 'react-redux'
import AxiosUtils from '../../../commons/axios/index'
import { getPmmlList } from '../../../redux/actions/pmml/pmmlAction'

const DeleteBtn = props => {

    const { visible, open, close } = ModalFunc()

    const { id } = props

    // 获取store对象中dispatch的引用
    const dispatch = useDispatch()

    function onOk () {
        let url = `/model/file/${id}`
        AxiosUtils.ajax({
            url: url,
            data: {
                isShowLoading: true
            },
            method: 'delete'
        }).then(res => {
            if(res.code === 0) {
                message.success("删除成功！")
                /** 创建成功则更新卡片列表 */
                dispatch(getPmmlList(0))
            } else {
                message.error("删除失败！")
            }
            /** 关闭弹窗 */
            close()
        })
    }

    return (
        <>
            <Button
                key="delete" 
                size="small"
                danger
                onClick={open}
            >
                删除
            </Button>
            <Modal
                title="删除文件"
                visible={visible}
                onOk={onOk}
                onCancel={close}
                width={450}
                maskClosable={false}
            >
                确定删除项目？
            </Modal>
        </>
    )
}

export default DeleteBtn